package com.crayon2f.test;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by feifan.gou@gmail.com on 2025/9/9 10:45.
 */
public class TEST2025_09 {

    public static void main(String[] args) {
        String directoryPath = "E:\\old\\video";
        File dir = new File(directoryPath);

        File[] files = dir.listFiles((d, name) ->
                name.toLowerCase().endsWith(".mkv") || name.toLowerCase().endsWith(".mp4")
        );

        for (File file : files) {
            String originalName = file.getName();
            String ext = originalName.substring(originalName.lastIndexOf('.'));
            String baseName = originalName.substring(0, originalName.lastIndexOf('.'));

            System.out.println("处理文件: " + originalName);

            // 方法1：直接字符串处理（最可靠）
            if (baseName.contains("绝命毒师")) {
                String season = "";
                String episode = "";

                // 查找"第"和"季"之间的内容
                int seasonStart = baseName.indexOf("第");
                int seasonEnd = baseName.indexOf("季");
                if (seasonStart != -1 && seasonEnd != -1 && seasonEnd > seasonStart) {
                    String seasonStr = baseName.substring(seasonStart + 1, seasonEnd);
                    season = "S" + String.format("%02d", parseNumber(seasonStr));
                }

                // 查找"第"和"集"之间的内容（从季后面开始找）
                int episodeStart = baseName.indexOf("第", seasonEnd);
                int episodeEnd = baseName.indexOf("集");
                if (episodeStart != -1 && episodeEnd != -1 && episodeEnd > episodeStart) {
                    String episodeStr = baseName.substring(episodeStart + 1, episodeEnd);
                    episode = "E" + String.format("%02d", parseNumber(episodeStr));
                }

                if (!season.isEmpty() && !episode.isEmpty()) {
                    String newName = "Breaking.Bad." + season + episode + ext;
                    File newFile = new File(dir, newName);

                    if (file.renameTo(newFile)) {
                        System.out.println("[✅成功] " + originalName + " → " + newName);
                    } else {
                        System.out.println("[❌失败] " + originalName);
                    }
                    continue;
                }
            }

            // 方法2：标准SxxExx格式
            Pattern pattern = Pattern.compile("(S\\d{1,2}E\\d{1,2})", Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(baseName);
            if (matcher.find()) {
                String se = matcher.group(1).toUpperCase();
                // 补零格式化
                se = se.replaceAll("S(\\d)E", "S0$1E").replaceAll("E(\\d)$", "E0$1");
                String newName = "Breaking.Bad." + se + ext;
                File newFile = new File(dir, newName);

                if (file.renameTo(newFile)) {
                    System.out.println("[✅成功] " + originalName + " → " + newName);
                } else {
                    System.out.println("[❌失败] " + originalName);
                }
                continue;
            }

            System.out.println("[⚠️跳过] 无法识别: " + originalName);
        }
    }

    private static int parseNumber(String str) {
        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException e) {
            // 如果是中文数字
            switch (str) {
                case "一":
                    return 1;
                case "二":
                    return 2;
                case "三":
                    return 3;
                case "四":
                    return 4;
                case "五":
                    return 5;
                case "六":
                    return 6;
                case "七":
                    return 7;
                case "八":
                    return 8;
                case "九":
                    return 9;
                case "十":
                    return 10;
                default:
                    return 1;
            }
        }
    }
}
